package com.ruoyi.project.bajiaostar.cooperationDataManagement.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.bajiaostar.cooperationDataManagement.domain.CooperationDataManagement;
import com.ruoyi.project.bajiaostar.cooperationDataManagement.domain.FindDataManagementReq;
import com.ruoyi.project.bajiaostar.cooperationDataManagement.domain.FindDataManagementResp;
import com.ruoyi.project.bajiaostar.cooperationDataManagement.domain.FindDataResp;
import com.ruoyi.project.bajiaostar.cooperationDataManagement.mapper.CooperationDataManagementMapper;
import com.ruoyi.project.bajiaostar.cooperationDataManagement.service.ICooperationDataManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 渠道分析管理Service业务层处理
 *
 * @author 谢少辉
 * @date 2025-08-20
 */
@Service
public class CooperationDataManagementServiceImpl implements ICooperationDataManagementService {
    @Autowired
    private CooperationDataManagementMapper cooperationDataManagementMapper;

    /**
     * 查询渠道分析管理
     *
     * @param id 渠道分析管理ID
     * @return 渠道分析管理
     */
    @Override
    public CooperationDataManagement selectCooperationDataManagementById(Long id) {
        return cooperationDataManagementMapper.selectCooperationDataManagementById(id);
    }

    /**
     * 查询渠道分析管理列表
     *
     * @param cooperationDataManagement 渠道分析管理
     * @return 渠道分析管理
     */
    @Override
    public List<CooperationDataManagement> selectCooperationDataManagementList(CooperationDataManagement cooperationDataManagement) {
        return cooperationDataManagementMapper.selectCooperationDataManagementList(cooperationDataManagement);
    }

    /**
     * 新增渠道分析管理
     *
     * @param cooperationDataManagement 渠道分析管理
     * @return 结果
     */
    @Override
    public int insertCooperationDataManagement(CooperationDataManagement cooperationDataManagement) {
        if(ObjectUtils.isEmpty(cooperationDataManagement.getCreateTime())){
            cooperationDataManagement.setCreateTime(DateUtils.getNowDate());
        }
        return cooperationDataManagementMapper.insertCooperationDataManagement(cooperationDataManagement);
    }

    /**
     * 修改渠道分析管理
     *
     * @param cooperationDataManagement 渠道分析管理
     * @return 结果
     */
    @Override
    public int updateCooperationDataManagement(CooperationDataManagement cooperationDataManagement) {
        cooperationDataManagement.setUpdateTime(DateUtils.getNowDate());
        return cooperationDataManagementMapper.updateCooperationDataManagement(cooperationDataManagement);
    }

    /**
     * 删除渠道分析管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCooperationDataManagementByIds(String ids) {
        return cooperationDataManagementMapper.deleteCooperationDataManagementByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除渠道分析管理信息
     *
     * @param id 渠道分析管理ID
     * @return 结果
     */
    @Override
    public int deleteCooperationDataManagementById(Long id) {
        return cooperationDataManagementMapper.deleteCooperationDataManagementById(id);
    }

    @Override
    public List<String> findAddPowderPlan() {
        return cooperationDataManagementMapper.findAddPowderPlan();
    }

    @Override
    public FindDataResp findDataManagement(FindDataManagementReq req) {
        List<FindDataManagementResp> respList = cooperationDataManagementMapper.findDataManagement(req);
        if (ObjectUtils.isEmpty(respList)) {
            return null;
        }
        //按照平台分组
 /*       Map<String, List<FindDataManagementResp>> channelTypeList = respList.stream()
                .collect(Collectors.groupingBy(FindDataManagementResp::getChannelType));*/

        List<FindDataManagementResp> channelList = new ArrayList<>();
        /*for (Map.Entry<String, List<FindDataManagementResp>> entry : channelTypeList.entrySet()) {
            FindDataManagementResp resp1 = new FindDataManagementResp();
            List<FindDataManagementResp> valueList = entry.getValue();
            resp1.setTestFlag(valueList.get(0).getTestFlag());
            resp1.setPlatformName(valueList.get(0).getPlatformName());
            resp1.setChannelType(valueList.get(0).getChannelType());
            int listSize = valueList.size();
            int resource = 0;
            BigDecimal channelRepetitionRate = BigDecimal.ZERO;
            BigDecimal idDelRate = BigDecimal.ZERO;
            BigDecimal openMouthRate = BigDecimal.ZERO;
            for (FindDataManagementResp resp : valueList) {
                BigDecimal resourceRebateRate=resp.getResourceRebateRate().divide(BigDecimal.valueOf(100));
                resource += new BigDecimal(resp.getResource()+"").multiply(resourceRebateRate).intValue();
                channelRepetitionRate =channelRepetitionRate.add(resp.getChannelRepetitionRate()) ;
                idDelRate =idDelRate.add(resp.getIdDelRate()) ;
                openMouthRate =openMouthRate.add(resp.getOpenMouthRate()) ;
            }
            resp1.setResource(new BigDecimal(resource).longValue());
            resp1.setChannelRepetitionRate(channelRepetitionRate.divide(new BigDecimal(listSize), 2, BigDecimal.ROUND_HALF_UP));
            resp1.setIdDelRate(idDelRate.divide(new BigDecimal(listSize), 2, BigDecimal.ROUND_HALF_UP));
            resp1.setOpenMouthRate(openMouthRate.divide(new BigDecimal(listSize), 2, BigDecimal.ROUND_HALF_UP));
            channelList.add(resp1);
        }*/
        /*FindDataManagementResp countResp = new FindDataManagementResp();
        //计算总计
        int resource = 0;
        int listSize = channelList.size();
        int channelRepetitionRate = 0;
        int idDelRate = 0;
        int openMouthRate = 0;
        for (FindDataManagementResp resp : channelList) {
            resource += resp.getResource();
            channelRepetitionRate += resp.getChannelRepetitionRate();
            idDelRate += resp.getIdDelRate();
            openMouthRate += resp.getOpenMouthRate();
        }
        countResp.setTestFlag("总计");
        countResp.setResource(new BigDecimal(resource).longValue());
        countResp.setChannelRepetitionRate(new BigDecimal(channelRepetitionRate).divide(new BigDecimal(listSize), 2, BigDecimal.ROUND_HALF_UP).longValue());
        countResp.setIdDelRate(new BigDecimal(idDelRate).divide(new BigDecimal(listSize), 2, BigDecimal.ROUND_HALF_UP).longValue());
        countResp.setOpenMouthRate(new BigDecimal(openMouthRate).divide(new BigDecimal(listSize), 2, BigDecimal.ROUND_HALF_UP).longValue());
        channelList.add(countResp);*/


        FindDataManagementResp countResp = new FindDataManagementResp();
        countResp.setTestFlag("否");
        channelList.add(countResp);

        //按照加粉方案分组
        Map<String, List<FindDataManagementResp>> addPowderPlanList = respList.stream()
                .collect(Collectors.groupingBy(FindDataManagementResp::getAddPowderPlan));

        List<FindDataManagementResp> addList = new ArrayList<>();
        for (Map.Entry<String, List<FindDataManagementResp>> entry : addPowderPlanList.entrySet()) {
            FindDataManagementResp resp1 = new FindDataManagementResp();
            List<FindDataManagementResp> valueList = entry.getValue();
            resp1.setTestFlag(valueList.get(0).getTestFlag());
            resp1.setPlatformName(valueList.get(0).getPlatformName());
            resp1.setChannelType(valueList.get(0).getChannelType());
            resp1.setAddPowderPlan(valueList.get(0).getAddPowderPlan());
            int listSize1 = valueList.size();
            int resource1 = 0;
            BigDecimal channelRepetitionRate1 = BigDecimal.ZERO;
            BigDecimal idDelRate1 = BigDecimal.ZERO;
            BigDecimal openMouthRate1 = BigDecimal.ZERO;
            for (FindDataManagementResp resp : valueList) {
                BigDecimal resourceRebateRate=resp.getResourceRebateRate().divide(BigDecimal.valueOf(100));
                resource1 += new BigDecimal(resp.getResource()+"").multiply(resourceRebateRate).intValue();
               // resource1 += resp.getResource();
                channelRepetitionRate1 = channelRepetitionRate1.add(resp.getChannelRepetitionRate());
                idDelRate1 = idDelRate1.add(resp.getIdDelRate());
                openMouthRate1 = openMouthRate1.add(resp.getOpenMouthRate());
            }
            resp1.setResource(new BigDecimal(resource1).divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP).longValue());
            resp1.setChannelRepetitionRate(channelRepetitionRate1.divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP));
            resp1.setIdDelRate(idDelRate1.divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP));
            resp1.setOpenMouthRate(openMouthRate1.divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP));
            addList.add(resp1);
        }


        FindDataManagementResp addCountResp = new FindDataManagementResp();
        //计算总计
        int listSize1 = addList.size();
        int resource1 = 0;
        BigDecimal channelRepetitionRate1 = BigDecimal.ZERO;
        BigDecimal idDelRate1 = BigDecimal.ZERO;
        BigDecimal openMouthRate1 = BigDecimal.ZERO;
        for (FindDataManagementResp resp : addList) {
            //resource1 += new BigDecimal(resp.getResource()+"").multiply(new BigDecimal(100).divide(resp.getResourceRebateRate(),2,BigDecimal.ROUND_HALF_UP)).intValue();
            resource1 += resp.getResource();
            channelRepetitionRate1 = channelRepetitionRate1.add(resp.getChannelRepetitionRate());
            idDelRate1 =idDelRate1.add(resp.getIdDelRate()) ;
            openMouthRate1 = openMouthRate1.add(resp.getOpenMouthRate());
        }
        addCountResp.setTestFlag("总计");
        addCountResp.setResource(new BigDecimal(resource1).longValue());
        addCountResp.setChannelRepetitionRate(channelRepetitionRate1.divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP));
        addCountResp.setIdDelRate(idDelRate1.divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP));
        addCountResp.setOpenMouthRate(openMouthRate1.divide(new BigDecimal(listSize1), 2, BigDecimal.ROUND_HALF_UP));
        addList.add(addCountResp);


        FindDataResp  dataResp = new FindDataResp();
        dataResp.setChannelList(channelList);
        dataResp.setAddList(addList);
        return dataResp;
    }
}
